﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecreate.Business;
using Ecreate.Model;
using System.Text.RegularExpressions;
using Ecreate.Popedom.Service.SpringImpl;

namespace Ecreate.Web.systemanage.order
{
    public partial class orderlistall : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLProductClass(ddlClass1, "-1");
                //BindDDLIsCheck(ddlIsCheck);

                AreaService service = new AreaService();
                BindDDLArea(ddlProvince, service.getProvinceArea());
                if (ddlProvince.SelectedIndex != -1)
                {
                    ddlProvince_SelectedIndexChanged(null, null);
                }
            }
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            string areaCode = ddlProvince.SelectedValue;
            AreaService service = new AreaService();
            BindDDLArea(ddlCity, service.getCityArea(areaCode));
            if (ddlCity.SelectedIndex != -1)
            {
                BindLevelTwoUser();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindLevelTwoUser();
        }

        private void BindLevelTwoUser()
        {
            string citycode = ddlCity.SelectedValue;
            string sql = "select userid,username from t_sysuser where citycode='" + citycode + "'";
            System.Data.DataTable dt = usercontrol.GG.GetTable(sql);
            System.Data.DataRow dr = dt.NewRow();
            dr["userid"] = "000000-0";
            dr["username"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            ddlLevelTwoUser.DataSource = dt;
            ddlLevelTwoUser.DataTextField = "username";
            ddlLevelTwoUser.DataValueField = "userid";
            ddlLevelTwoUser.DataBind();
        }

        private void BindDDLArea(DropDownList ddl, IList list)
        {
            t_area area = new t_area();
            area.AreaName = "全部";
            area.AreaCode = "-100";
            list.Insert(0, area);
            BindDDL(ddl, list, "AreaName", "AreaCode");
        }

        //private void BindDDLIsCheck(DropDownList ddl)
        //{
        //    IList list = new List<TextValue>();
        //    list.Add(GetTextValue("全部", "-1"));
        //    list.Add(GetTextValue("已确认", "1"));
        //    list.Add(GetTextValue("未确认", "0"));
        //    BindDDL(ddl, list, "Text", "Value");
        //}

        private TextValue GetTextValue(string stext, string svalue)
        {
            TextValue tv = new TextValue();
            tv.Text = stext;
            tv.Value = svalue;
            return tv;
        }
        
        private void BindDDLProductClass(DropDownList ddl, string parentclassid)
        {
            ProductClassService service = new ProductClassService();
            IList list = service.getProductClassByParentID(parentclassid);
            ProductClass pc = new ProductClass();
            pc.Classname = "全部";
            pc.Classid = "0";
            list.Insert(0, pc);
            BindDDL(ddl, list, "classname", "classid");
        }

        private void BindDDL(DropDownList ddl, IList list, string TextField, string ValueField)
        {
            ddl.DataSource = list;
            ddl.DataTextField = TextField;
            ddl.DataValueField = ValueField;
            ddl.DataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            gv.Visible = true;
            BindData();
        }

        private void BindData()
        {
            string sql = "select OrderID,KickOrderID,SN,Times,SellAddress,SellPhoneNumber,ProductName,Price,CustName,CustPhoneNumber,CustAddress,PayAccount,PayDate,IsCheckName,CheckDate from v_order where IsCheck='1'";
            if (ddlProvince.SelectedIndex > 0)
            {
                sql += " and Province='" + ddlProvince.SelectedValue.ToString() + "'";
            }
            if (ddlCity.SelectedIndex > 0)
            {
                sql += " and City='" + ddlCity.SelectedValue.ToString() + "'";
            }
            if (ddlCity.SelectedIndex > 0)
            {
                sql += " and CustomerID=(select cus_id from t_customer where level_two_userid='" + ddlCity.SelectedValue.ToString() + "')";
            }
            if (txtQQ.Text.Length > 0)
            {
                sql += " and QQ='" + txtQQ.Text + "'";
            }
            if (txtOrderID.Text.Length > 0)
            {
                sql += " and OrderID='" + txtOrderID.Text + "'";
            }
            if (txtPhoneNumber.Text.Length > 0)
            {
                sql += " and SellPhoneNumber='" + txtPhoneNumber.Text + "'";
            }
            if (ddlClass1.SelectedIndex > 0)
            {
                sql += " and ProductClass1='" + ddlClass1.SelectedValue.ToString() + "'";
            }
            //if (ddlIsCheck.SelectedIndex > 0)
            //{
            //    sql += " and IsCheck='" + ddlIsCheck.SelectedValue.ToString() + "'";
            //}
            if (txtPayDate1.Text.Length > 0)
            {
                sql += " and convert(char(10),PayDate,120)>='" + txtPayDate1.Text + "'";
            }
            if (txtPayDate2.Text.Length > 0)
            {
                sql += " and convert(char(10),PayDate,120)<='" + txtPayDate2.Text + "'";
            }
            if (txtCheckDate1.Text.Length > 0)
            {
                sql += " and convert(char(10),CheckDate,120)>='" + txtCheckDate1.Text + "'";
            }
            if (txtCheckDate2.Text.Length > 0)
            {
                sql += " and convert(char(10),CheckDate,120)<='" + txtCheckDate2.Text + "'";
            }
            sql += " order by OrderID";
            System.Data.DataTable dt = usercontrol.GG.GetTable(sql);
            gv.DataSource = dt;
            gv.DataBind();
        }

        protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                foreach (TableCell cell in e.Row.Cells)
                {
                    string text = cell.Text;
                    cell.Text = GetHeaderText(text);
                }
            }
        }

        private string GetHeaderText(string text)
        {
            string headertext = text;
            string[] fields = "OrderID,KickOrderID,SN,Times,SellAddress,SellPhoneNumber,ProductName,Price,CustName,CustPhoneNumber,CustAddress,PayAccount,PayDate,IsCheckName,CheckDate".Split(new char[] { ',' });
            string[] titles = "订单编号,被此订单顶掉,当前排序,被顶掉次数,销售地址,销售电话,产品名称,价格,消费者姓名,消费者电话,消费者收件地址,付款账号,付款日期,付款确认,确认日期".Split(new char[] { ',' });
            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i] == text)
                {
                    headertext = titles[i];
                    break;
                }
            }
            return headertext;
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            txtPayDate1.Text = Calendar1.SelectedDate.ToString("yyyy-MM-dd");
        }
        protected void Calendar2_SelectionChanged(object sender, EventArgs e)
        {
            txtPayDate2.Text = Calendar2.SelectedDate.ToString("yyyy-MM-dd");
        }
        protected void Calendar3_SelectionChanged(object sender, EventArgs e)
        {
            txtCheckDate1.Text = Calendar3.SelectedDate.ToString("yyyy-MM-dd");
        }
        protected void Calendar4_SelectionChanged(object sender, EventArgs e)
        {
            txtCheckDate2.Text = Calendar4.SelectedDate.ToString("yyyy-MM-dd");
        }
    }
    public class TextValue
    {
        string _text;
        string _value;
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
    }
}
